package com.core.config;

import com.core.filter.JwtAuthenticationFilter;
import com.core.handler.AccessDeniedHandlerHandler;
import com.core.handler.AuthenticationEntryPointHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

/**
 * @author : HUANG
 * @create : 2025/5/31 22:47
 * @function :
 */
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private JwtAuthenticationFilter jwtAuthenticationFilter;
    @Autowired
    private AccessDeniedHandlerHandler accessDeniedHandler;
    @Autowired
    private AuthenticationEntryPointHandler authenticationEntryPoint;

    /**
     * 添加密码验证方式的bean,将BCryptPasswordEncoder注入容器
     *
     * @return
     */
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    @Override
    protected AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //关闭csrf
        http.csrf().disable()
                //不通过Session获取SecurityContext
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()
                // 放行登录/登出接口
                .antMatchers("/login", "/logout/**","/test").permitAll()

                // —— 以下所有与 Swagger UI、接口文档相关的 URL 都要放行 ——
                // 1. swagger-ui.html 页面
                .antMatchers("/swagger-ui/index.html").permitAll()
                .antMatchers("/swagger-ui/**").permitAll()
                // 2. swagger-ui 里的静态资源 (js/css/fonts)
                .antMatchers("/swagger-ui.html").permitAll()
                // 3. Springfox 2.x 的接口文档地址
                .antMatchers("/v2/api-docs", "/v2/api-docs/**").permitAll()
                .antMatchers("/swagger-resources/**").permitAll()
                // 4. Springfox 3.x 的接口文档地址 (如果同时启用了 v3 版本)
                .antMatchers("/v3/api-docs", "/v3/api-docs/**").permitAll()
                // 5. WebJar 里放的 swagger-ui 静态资源
                .antMatchers("/webjars/**").permitAll()
                // 6. 额外的 swagger-resources 配置接口（Springfox 有时会调用这些）
                .antMatchers(
                        "/swagger-resources/configuration/ui",
                        "/swagger-resources/configuration/security"
                ).permitAll()
                // —— 以上放行完毕 ——
                // 除上面外的所有请求全部需要鉴权认证
                .anyRequest().authenticated();
        http.exceptionHandling()
                .accessDeniedHandler(accessDeniedHandler)
                .authenticationEntryPoint(authenticationEntryPoint);
        // 允许跨域
        http.cors();
        // 启用拦截器，并且添加到UsernamePasswordAuthenticationFilter之前
        http.addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
    }
}
